package hanlder

import (
	"github.com/gin-gonic/gin"
	"strconv"
	"video-master/models"
)

func Add(c *gin.Context) {
	file := c.PostForm("file")
	content := c.PostForm("content")
	if file == "" || content == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}
	data := models.Video{
		File:    file,
		Content: content,
	}
	err := models.DB.Create(&data).Debug().Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	} else {
		c.JSON(200, gin.H{
			"code": 200,
			"msg":  "添加成功",
		})
		return
	}
}
func List(c *gin.Context) {
	page, _ := strconv.Atoi(c.Query("page"))
	size, _ := strconv.Atoi(c.Query("size"))
	offset := (page - 1) * size
	var goods []models.Video
	models.DB.Offset(offset).Limit(size).Find(&goods).Debug()

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "success",
		"data": goods,
	})
	return
}
